//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.basic.action.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.action.BaseAction;
import net.mingsoft.basic.biz.IAppBiz;
import net.mingsoft.basic.entity.ManagerEntity;
import net.mingsoft.basic.strategy.ILoginStrategy;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.config.MSProperties;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

@Api(
        tags = {"前端-基础接口"}
)
@Controller
@RequestMapping({"/${ms.manager.path}"})
public class LoginAction extends BaseAction {
    @Autowired
    private IAppBiz appBiz;
    @Autowired
    private ILoginStrategy loginStrategy;

    public LoginAction() {
    }

    @ApiOperation("加载管理员登录界面")
    @GetMapping({"/login"})
    public String login(HttpServletRequest request) {
        String managerPath = MSProperties.manager.path;
        Subject currentSubject = SecurityUtils.getSubject();
        ManagerEntity user = (ManagerEntity)currentSubject.getPrincipal();
        if (user != null && currentSubject.isAuthenticated()) {
            return "redirect:" + managerPath + "/index.do";
        } else {
            request.setAttribute("app", BasicUtil.getApp());
            return "/login";
        }
    }

    @ApiOperation("验证登录")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "managerName",
            value = "帐号",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "managerPassword",
            value = "密码",
            required = true,
            paramType = "query"
    )})
    @PostMapping({"/login"})
    @ResponseBody
    public ResultData login(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, HttpServletResponse response) {
        this.LOG.debug("basic checkLogin");
        if (!this.checkRandCode()) {
            return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("rand.code")}));
        } else {
            return this.loginStrategy.login(manager) ? ResultData.build().success() : ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("manager.name.or.password")}));
        }
    }
}
